﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.SurveyManage
{
    public partial class SurveyList : Common.PageBase
    {
        public string ModifyUrl = "SurveyModify.aspx?SURVEY_ID={0}";
        public string SurveyItemsListUrl = "SurveyItemsList.aspx?SURVEY_ID={0}&SURVEY_NAME={1}";
        T_SURVEY_BLL sBll = new T_SURVEY_BLL();
        T_SURVEY_ITEM_BLL iBll = new T_SURVEY_ITEM_BLL();
        T_SURVEY_ITEM_OPTION_BLL siopBll = new T_SURVEY_ITEM_OPTION_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPopCalender(this.Page, txtSDATEEnd);
            PageHelper.SetPopCalender(this.Page, txtSDATEBegin);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetUrlQueryToControl();
                gvDataBinding();
            }
        }
        #region 页面初始化数据绑定

        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["SURVEY_ID"]))
            {
                sql.Append(" and SURVEY_ID = '" + Request.QueryString["SURVEY_ID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SNAME"]))
            {
                sql.Append(" and SNAME like '%" + Request.QueryString["SNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SDATEBegin"]))
            {
                sql.Append(" and " + sBll._dal.GetBeginDateString("SDATE", Request.QueryString["SDATEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SDATEBegin"]))
            {
                sql.Append(" and " + sBll._dal.GetEndDateString("SDATE", Request.QueryString["SDATEEnd"].Trim()) + " ");
            }
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            return sql;
        }
        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_SURVEY_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)sBll.GetCount(sql.ToString());
            list = sBll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);
            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "SURVEY_ID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_SURVEY_Entity entity = new T_SURVEY_Entity();
            entity.SURVEY_ID = StringHelper.ToInt((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());
            long rel = sBll.Delete(entity);
            if (rel > 0)
            {
                iBll.Delete("SURVEY_ID", entity.SURVEY_ID.ToString());
                siopBll.Delete("SURVEY_ID", entity.SURVEY_ID.ToString());
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {
                PageHelper.ShowMessage(this.Page, "问卷删除失败！");
            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
